<style>
    /*#ProductionOrder-edit .layui-table-body {*/
    /*    margin-right: -17px;*/
    /*}*/
    .layui-layer-title {
        display: flex;
        align-items: center;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label">报工编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入报工编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">上机编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="computerPracticeCode" class="layui-input" type="text" placeholder="请输入上机编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">产线</label>
                                    <div class="layui-input-inline mr0">
                                        <select  id="productionLineId" name="productionLineId" lay-filter="productionLineId"  lay-search required></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">生产计划编号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="productionPlanCode" class="layui-input" type="text" placeholder="请输入生产计划编号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">成品捆包号</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="finishedProductBundleCode" class="layui-input" type="text" placeholder="请输入成品捆包号" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">客户名称</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="customerName" class="layui-input" type="text"
                                               placeholder="请输入客户名称"/>
                                    </div>
                                </div>
<!--                                <div class="layui-inline">-->
<!--                                    <label class="layui-form-label">原始捆包号</label>-->
<!--                                    <div class="layui-input-inline mr0">-->
<!--                                        <input name="userBundleCode" class="layui-input" type="text"-->
<!--                                               placeholder="请输入捆包号"/>-->
<!--                                    </div>-->
<!--                                </div>-->
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">品种</label>
                                    <div class="layui-input-inline mr0">
                                        <select  id="productVarietyId" name="productVarietyId" lay-filter="productVarietyId"  lay-search required></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">牌号</label>
                                    <div class="layui-input-inline mr0">
                                        <select  id="productMarkId" name="productMarkId" lay-filter="productMarkId"  lay-search required></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">生产商</label>
                                    <div class="layui-input-inline mr0">
                                        <select  id="productProducerId" name="productProducerId" lay-filter="productProducerId"  lay-search required></select>
                                    </div>
                                </div>

                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="lay-spread-box">
                    <div class="spread-btn">
                        <span class="spread-span">展开</span>
                        <i class="spread-more"></i>
                    </div>
                </div>

                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="ProductionOrderBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt"></div>
                    </div>
                </div>
            </div>

            <!-- 表格 -->
            <table class="layui-table" id="ProductionOrderTable" lay-filter="ProductionOrderTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="ProductionOrderTableBar">
    <!-- {{# if(d.status == 1){ }} -->
    <!-- {{# } }} -->
    <a class="layui-btn layui-btn-xs layui-btn" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn" lay-event="detail">详情</a>
    {{# if(d.status === '1'){ }}
    <a class="layui-btn layui-btn-xs layui-btn" lay-event="AddQualityTesting">质检</a>
    {{# } }}
</script>

<style>
    #ProductionOrder-form .layui-disabled {
        color: #393d49 !important;
    }
</style>

<!-- js部分 -->
<script>
    layui.use(["layer", "form", "util", "table", "admin", "laytpl", "laydate", "tableTreeDj"], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var util = layui.util;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var tableTree = layui.tableTreeDj;



        // 初始化车间下拉框
        // admin.initDataSelectPro(
        //     "warehouse/warehouse/productionLine/list?page=1&limit=9999",  // 替换为实际请求地址
        //     { limit: 9999 }, // 参数
        //     "workshopId",  // 目标下拉框ID
        //     "客户",  // 下拉框标题
        //     async (d, obj) => {
        //         // 可以在这里处理选中后的操作
        //     },
        //     "workshopName"  // 使用 customerName 作为显示文本
        // );

        // 初始化产线下拉框
        admin.initDataSelectPro(
            "warehouse/warehouse/productionLine/list?page=1&limit=9999",  // 替换为实际请求地址
            { limit: 9999 }, // 参数
            "productionLineId",  // 目标下拉框ID
            "产线",  // 下拉框标题
            async (d, obj) => {
                // 可以在这里处理选中后的操作
            },
            "productionLineName"  // 使用 name 作为显示文本
        );

        admin.initDataSelectPro(
            "warehouse/warehouse/admin/productVariety/list",
            {
                limit: 99999,

            },
            "productVarietyId",
            "品种",
            (d) => {
                if (d.value) {
                    productVarietyId = d.value;
                }
            },
            "varietyName"
        );

        admin.initDataSelectPro(
            "warehouse/warehouse/admin/productMark/list",
            {
                limit: 99999,

            },
            "productMarkId",
            "牌号",
            (d) => {
                if (d.value) {
                    productMarkId = d.value;
                }
            },
            "markName"
        );

        admin.initDataSelectPro(
            "warehouse/warehouse/admin/productProducer/list",
            {
                limit: 99999,

            },
            "productProducerId",
            "生产商",
            (d) => {
                if (d.value) {
                    processingType = d.value;
                }
            },
            "producerName"
        );

        // 渲染表格
        var insTb = table.render({
            elem: "#ProductionOrderTable",
            url: config.base + "warehouse/warehouse/processingReport/list",
            toolbar: true,
            defaultToolbar: ["filter"],
            where: { access_token: config.getToken().access_token },
            page: true,
            cellMinWidth: 100,
            cols: [
                [
                    { type: "numbers",title: "序号"},
                    { field: "code", align: "center", title: "报工记录编号" },
                    { field: "computerPracticeCode", align: "center", title: "上机记录编号" },
                    { field: "productionPlanCode", align: "center", title: "生产计划编号" },
                    {field: "customerName", align: "center", title: "客户名称"},
                    // {field: "userBundleCode", align: "center", title: "捆包号"},
                    // { field: "customerProductCode", align: "center", title: "客户产品料号" },
                   
                    { field: "creatorName", align: "center", title: "报工人" },
                    { field: "createTime", align: "center", title: "报工时间" },
                    // 状态1.待审核，2.待激活，3.待领料，4.待报工，5.生产中，6.暂停，7.终止，8.完成
                    {
                        field: "status",
                        align: "center",
                        title: "状态",
                        templet: function (d) {
                            return d.status == 1 ? "待质检" : "已质检";
                        }
                    },
                    // { field: "creatorName", align: "center", title: "创建人" },
                    // { field: "createTime", align: "center", title: "创建时间" },
                    { align: "center", fixed: "right", toolbar: "#ProductionOrderTableBar", fixed: "right",title: "操作", width: 200 }
                    //
                ]
            ]
        });

        // 工具条点击事件
        table.on("tool(ProductionOrderTable)", function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === "detail") {
                admin.req(
                    "warehouse/warehouse/processingReport/selectProcessingReport",
                    {
                        id: data.id
                    },
                    (res) => {
                        if (res.code === 200) {
                            openFormFun("detail", res.data);
                        }
                    }
                );
            } else if (layEvent === "edit") {
                admin.req(
                    "warehouse/warehouse/processingReport/selectProcessingReport",
                    {
                        id: data.id
                    },
                    (res) => {
                        if (res.code === 200) {
                            openFormFun("edit", res.data);
                        }
                    }
                );
            } else if(layEvent === "AddQualityTesting"){
                admin.formOpen({
                    area: "90%",
                    offset: "5%",
                    title: "添加加工质检记录",
                    path: "components/warehouse/product/warehouseQualityTestingForm.html",
                    success: function () {
                        parentData.eventName = 'processingReportAdd';
                        parentData.processingReportId = data.id;
                    }
                });

            }else if (layEvent === "audit") {
                // 确认是否审核通过
                layer.open({
                    title: "审核",
                    content: "是否审核通过？",
                    btn: ["审核通过", "关闭"],
                    yes: function (index) {
                        console.log(index);
                        admin.req("warehouse//warehouse/productionSchedulingPlan/updateProductionSchedulingPlan", { id: data.id, status: 2 }, (res) => {
                            if (res.code === 200) {
                                layer.msg("审核通过");
                                layer.close(index);
                                insTb.reload();
                            }
                        });
                    }
                });
            }
        });

        // 搜索
        form.on("submit(formSubSearchLog)", function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
        });

        // 重置搜索
        form.on("submit(resets)", function (data) {
            insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
        });

        form.render("select");

        // 添加按钮点击事件
        $("#ProductionOrderBtnAdd").click(function () {
            openFormFun("add");
        });

        function openFormFun(eventName, data) {
            admin.formOpen({
                area: "90%",
                offset: "5%",
                title: "添加加工报工记录",
                path: "components/warehouse/product/warehouseProcessingForm.html",
                success: function () {
                    parentData.eventName = eventName;
                    parentData.data = data;
                }
            });
        }
    });
</script>
